/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package simulator.engine;

import java.sql.SQLException;
import java.util.ArrayList;

/**
 * This class is used for the inventory class
 * it contains a furniture object for each furnitureID used in simulation
 * @author 490060
 */
public class Structure 
{
    private final int ID = 0;
    private final int QUANTITY = 1;
    private final int ENERGY_USAGE = 2;
    private final int NAME = 3;
    private final int CATEGORY = 4;

    
    private int profileID;
    db.MysqlAdapter db = new db.MysqlAdapter();
    
    private ArrayList<Furniture> furnitureTable = new ArrayList<Furniture>(); 
    private ArrayList<String[]> dbString;
    
    public Structure(int profileID) throws SQLException
    {
        this.profileID = profileID;
        dbString = db.getHouseProfile(profileID);
        for(int i = 0; i < dbString.size(); i++)
        {
            addItem(Integer.parseInt(dbString.get(i)[ID]), Integer.parseInt(dbString.get(i)[QUANTITY]), 
                    Float.parseFloat(dbString.get(i)[ENERGY_USAGE]),  dbString.get(i)[NAME], dbString.get(i)[CATEGORY]);
        }
        
    }
    
    /**
     * if item exist, adds to quantity, else add new item to catalog
     * @param itemID 
     */
    private void addItem(int itemID, int quantity, float energyUsage, String name, String category)
    {
        boolean itemExists = false;
        //Check if item exists
        for(int i = 0; i < furnitureTable.size(); i++)
        {
            if(itemID == furnitureTable.get(i).getItemID())
            {
                furnitureTable.get(i).setQuantity(quantity);
                itemExists = true;
            }
        }

        if(!itemExists)
        {
            furnitureTable.add(new Furniture(itemID, quantity, category, name, energyUsage));
        }
    }
    
    public int getNumberOfDifferentItemsInStructure()
    {
        return furnitureTable.size();
    }
    
    public float getMaxEnergyUsage()
    {
        float output = 0;
        for(int i = 0; i < furnitureTable.size(); i++)
        {
            output += furnitureTable.get(i).getMaxEnergyUsage();
        }
        return output;
    }
    
    public float getMaxEnergyUsage(String category)
    {
        float output = 0;
        for(int i = 0; i < furnitureTable.size(); i++)
        {
            if(furnitureTable.get(i).getCategory().equals(category))
                output += furnitureTable.get(i).getMaxEnergyUsage();
        }
        
        return output;
    }
    
    public static void main(String[] args) throws SQLException
    {
        Structure s = new Structure(2);
        System.out.println(s.getMaxEnergyUsage());
        
    }
    
    
}
